package com.game.base.app.base;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;

import com.game.base.app.App;
import com.game.base.app.callback.ItemOnClick;

import java.util.ArrayList;

/**
 * 适配器基类
 * Created by Su on 2019/5/22.
 */
public abstract class BaseAdapter<M, B extends ViewDataBinding> extends RecyclerView.Adapter {
    protected ArrayList<M> items;
    private ItemOnClick onClickInte;
    public Activity mActivity;

    public BaseAdapter() {
        items = new ArrayList<>();
    }

    public BaseAdapter(Activity activity) {
        mActivity = activity;
        items = new ArrayList<>();
    }

    public BaseAdapter(Activity activity, ItemOnClick itemOnclickInte) {
        mActivity = activity;
        this.items = new ArrayList<>();
        this.onClickInte = itemOnclickInte;
    }

    public BaseAdapter(ArrayList<M> items) {
        this.items = items;
    }

    public BaseAdapter(ArrayList<M> items, ItemOnClick itemOnclickInte) {
        this.items = items;
        this.onClickInte = itemOnclickInte;
    }

    @Override
    @NonNull
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        B binding = DataBindingUtil.inflate(LayoutInflater.from(App.sInstance), this.getLayoutResId(viewType), parent, false);
        return new BaseViewHolder(binding.getRoot());
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        B binding = DataBindingUtil.getBinding(holder.itemView);

        if (onClickInte != null)
            binding.getRoot().setOnClickListener(v -> onClickInte.onItemClick(v.getId(), position, items.get(position)));
        this.onBindItem(binding, this.items.get(position), position);
        binding.executePendingBindings();
    }

    @Override
    public int getItemCount() {
        if (items == null) {
            return 0;
        }
        return this.items.size();
    }

    protected abstract int getLayoutResId(int viewType);

    protected abstract void onBindItem(B binding, M item, int position);

    public void add(M m) {
        items.add(m);
    }

    public ArrayList<M> getItems() {
        return items;
    }

    public M getItem(int position) {
        if (position < 0 || position > items.size()) {
            return null;
        } else {
            return items.get(position);
        }
    }

    public void refreshData(ArrayList<M> list) {
        items.clear();
        if (list != null) {
            items.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void removeItem(int position) {
        if (items != null & position >= 0 && position < items.size()) {
            items.remove(position);
        }
        notifyDataSetChanged();
    }

    public void setItems(ArrayList<M> items) {
        this.items = items;
        notifyDataSetChanged();
    }
}
